package com.ruoyi.system.hbos.tool;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.ruoyi.system.hbos.domain.BaseEntityHbos;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * http请求工具类
 * @author xmd
 *
 */
public class HttpInvoker {

    /**
     * 发送请求，拉取参数
     * @param sUrl
     * @param sMethod
     * @param sOutput
     * @return
     */
    public static JSONObject exec(String sUrl, String sMethod, String sOutput, String appKey, BaseEntityHbos baseEntityHobs) {
        JSONObject json = null;
        StringBuffer buffer = new StringBuffer();

        HttpURLConnection con = null;
        try {
            // 基础参数设置
            URL url = new URL(sUrl);
            con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);  // POST请求必须设置
            con.setDoInput(true);   // POST请求必须设置
            con.setUseCaches(false);
            con.setRequestMethod(sMethod);
            con.setRequestProperty("Charset", "UTF-8");

            // 设置请求头
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("App-Key", appKey);

            // 设置请求体
            Gson gson = new Gson();
            if(baseEntityHobs != null){
                String bobsBaseEntity_json = gson.toJson(baseEntityHobs);
                // 发送请求体
                try (OutputStream os = con.getOutputStream()) {
                    byte[] input = bobsBaseEntity_json.getBytes("utf-8");
                    os.write(input, 0, input.length);
                }
            }

            // 设置超时时间
            con.setReadTimeout(60000);
            con.setConnectTimeout(60000);

            // 读取
            if (sOutput != null) {
                OutputStream os = con.getOutputStream();
                try {
                    os.write(sOutput.getBytes("UTF-8"));
                } catch (Exception e) {
                } finally {
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }

            InputStream is = null;
            InputStreamReader inputReader = null;
            BufferedReader reader = null;
            try {
                is = con.getInputStream();
                inputReader = new InputStreamReader(is, "UTF-8");
                reader = new BufferedReader(inputReader);
                String temp;
                while ((temp = reader.readLine()) != null) {
                    buffer.append(temp);
                }
            } catch (Exception e) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }
                if (inputReader != null) {
                    try {
                        inputReader.close();
                    } catch (IOException e) {
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                    }
                }
            }

            // con.disconnect();
            json = JSONObject.parseObject(buffer.toString());

            if (json != null) {
            } else {
            }
        } catch (IOException e) {
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }

        return json;
    }

    /**
     * sendPost(待定)
     * @param url
     * @param param
     * @return
     */
    public static String sendPost(String url, String param){
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            //注意在传送json数据时， Content-Type的值
            conn.setRequestProperty("Content-Type",
                    "application/json");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Charset", "UTF-8");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.write(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

}